<template>
	<view class="code-login">
		<view class="title">
			设置密码
		</view>
		<u--form labelPosition="top" :model="model" :rules="rules" ref="uForm" :labelStyle="labelStyle">
			<u-form-item label="账号" prop="account">
				<u--input :customStyle="accountCustomStyle" v-model="model.account" border="none"
					placeholder="请输入手机号" />
			</u-form-item>
			<u-form-item label="验证码" prop="code">
				<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
				<!-- #ifndef APP-NVUE -->
				<u-input :customStyle="accountCustomStyle" v-model="model.code" border="none" placeholder="请输入验证码">
				<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					<u--input :customStyle="accountCustomStyle" v-model="model.code" border="none" placeholder="请输入验证码">
					<!-- #endif -->
						<template slot="suffix">
							<u-code :seconds="seconds" @end="codeEnd" @start="codeStart" ref="uCode"
								@change="codeChange"></u-code>
							<text class="send-code" @click="handleClickSendCode">{{code}}</text>
						</template>
				<!-- #ifndef APP-NVUE -->
				</u-input>
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				</u--input>
				<!-- #endif -->
			</u-form-item>
			<u-form-item label="密码" prop="password">
				<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
				<!-- #ifndef APP-NVUE -->
				<u-input :type="passwordType" :customStyle="accountCustomStyle" v-model="model.password" border="none"
					placeholder="请输入密码">
				<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					<u--input :customStyle="accountCustomStyle" v-model="model.password" border="none"
						placeholder="请输入密码">
					<!-- #endif -->
						<template slot="suffix">
							<u-icon name="eye" @tap="passwordType=passwordType==='text'?'password':'text'"></u-icon>
							<!-- <u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button> -->
						</template>
				<!-- #ifndef APP-NVUE -->
				</u-input>
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				</u--input>
				<!-- #endif -->
			</u-form-item>
			<u-form-item label="二次确认密码" prop="passwords">
				<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
				<!-- #ifndef APP-NVUE -->
				<u-input :type="passwordsType" :customStyle="accountCustomStyle" v-model="model.passwords" border="none"
					placeholder="请输入密码">
				<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					<u--input :customStyle="accountCustomStyle" v-model="model.passwords" border="none"
						placeholder="请输入密码">
					<!-- #endif -->
						<template slot="suffix">
							<u-icon name="eye" @tap="passwordsType=passwordsType==='text'?'password':'text'"></u-icon>
							<!-- <u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button> -->
						</template>
				<!-- #ifndef APP-NVUE -->
				</u-input>
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				</u--input>
				<!-- #endif -->
			</u-form-item>
			<!-- <view class="forgot-password" @click="handleClickForgotPassword">忘记密码</view> -->
		</u--form>
		<view class="account-button" @click="handleClickAccountLogin">登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				labelStyle: {
					fontSize: "32rpx",
					fontWeight: 500,
					marginLeft: "32rpx",
					width: "200rpx"
				},
				accountCustomStyle: {
					height: "96rpx",
					borderRadius: "24rpx",
					background: "rgba(246, 248, 252, 1)",
					padding: "0 36rpx",
				},
				model: {
					account: "",
					password: "",
					passwords: "",
				},
				passwordType: "password",
				passwordsType: "password",
				rules: {
					account: [{
							required: true,
							message: '请输入账号',
							trigger: ['change', 'blur'],
						},
						// {
						// 	// 自定义验证函数，见上说明
						// 	validator: (rule, value, callback) => {
						// 		// 上面有说，返回true表示校验通过，返回false表示不通过
						// 		// uni.$u.test.mobile()就是返回true或者false的
						// 		return uni.$u.test.mobile(value);
						// 	},
						// 	message: '账号不正确',
						// 	// 触发器可以同时用blur和change
						// 	trigger: ['change', 'blur'],
						// }
					],
					code: [{
						required: true,
						message: "请输入验证码",
						trigger: ['change', 'blur']
					}],
					password: [{
						required: true,
						message: "请输入密码",
						trigger: ['change', 'blur']
					}],
					passwords: [{
							required: true,
							message: "请输入二次确认密码",
							trigger: ['change', 'blur']
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								console.log(this.model.password, value);
								return this.model.password === value
							},
							message: '二次密码不一样',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				},
				code: "发送验证码",
				seconds: 60
			}
		},
		onReady() {
				//onReady 为uni-app支持的生命周期之一
		    	this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			/**
			 * 账号登录
			 */
			handleClickAccountLogin() {
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过')
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
				console.log("账号登录");
			},
			/**
			 * 发送验证码
			 */
			handleClickSendCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			codeEnd() {

			},
			codeStart() {

			},
			codeChange(text) {
				this.code = text;
			},
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifdef H5 */
	page {
		background-color: $page-color-base2;
	}
	/* #endif */
	
	.code-login {
		position: relative;
		background-color: $page-color-base2;
		width: 100%;

		.title {
			padding-top: 36rpx;
			margin: 0 36rpx 36rpx 72rpx;
			font-size: 60rpx;
			font-weight: 500;
		}

		::v-deep .u-form {
			margin: 40rpx;
		}

		.account {
			border: 1rpx red solid;
		}

		.forgot-password {
			margin-right: 50rpx;
			text-align: right;
			font-size: 28rpx;
			font-weight: 400;
		}

		.to-register {
			text-align: center;
			font-size: 28rpx;
			font-weight: 400;

			text {

				color: rgba(76, 212, 230, 1);
			}
		}

		.account-button {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 44px;
			background: $theme-color;
			margin: 40rpx auto;
			text-align: center;
			border-radius: 24rpx;
			color: #FFF;
			font-size: 32rpx;
			margin: 40rpx;
		}

		.send-code {
			font-size: 28rpx;
			font-weight: 400;
			color: rgba(76, 212, 230, 1);
		}
	}
</style>